//
//  FeedController.m
//  SpendFunk
//
//  Created by bm on 4/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FeedController.h"
#import "FeedCell.h"
#import "SpendFunkAppDelegate.h"
#import "image.h"
#import "Global.h"

@implementation FeedController


#pragma mark -
#pragma mark Initialization
-(id)init{
	return self;
}

-(void)loadView{
	[super loadView];
	
	[self initView];
}

-(void)initView{
	//back
	NSString *path = nil;
	UIImage *img = nil;
	
	////
	path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	////
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 25, img.size.width , img.size.height)];
	[imgvBkgr setImage:img];
	[self.view addSubview:imgvBkgr];
	[imgvBkgr setUserInteractionEnabled:YES];
	[imgvBkgr release];
		
	///
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_LOAD];
	img = [UIImage imageNamed:path];	
	m_pBtnRefresh = [[UIButton alloc] initWithFrame:CGRectMake(320-5-img.size.width, -14, img.size.width, img.size.height)];
	[m_pBtnRefresh setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnRefresh setBackgroundColor:[UIColor clearColor]];
	m_pBtnRefresh.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnRefresh addTarget:nil action:@selector(onClickRefresh) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnRefresh];
	
	int nHRow = 180;
	m_pMainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 24, 320, 2*nHRow) style:UITableViewStylePlain];
	m_pMainTableView.delegate = self;
	m_pMainTableView.dataSource = self;
	m_pMainTableView.rowHeight = nHRow;
	[m_pMainTableView setShowsVerticalScrollIndicator:NO];
	[m_pMainTableView setBackgroundColor:[UIColor clearColor]];
	[self.view addSubview:m_pMainTableView];
	//[m_pMainTableView release];
	[m_pMainTableView reloadData];
	m_pMainTableView.scrollEnabled = YES;
	
}

-(void)onClickRefresh{

}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}
- (void)setArrFeed:(NSMutableArray*)arrFeed{
	if (m_pArrFeed) {
		[m_pArrFeed release];
		m_pArrFeed = nil;
	}
	m_pArrFeed = [arrFeed retain];
	[m_pMainTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
   // return 3;
	if (m_pArrFeed) {
		return [m_pArrFeed count];
	}
	else{
		return 0;
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    FeedCell *cell = (FeedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	//du lieu se lay tu mang m_pArrContent
    [cell setName:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kTitle]];
	[cell setLocation:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kAddress]];
	[cell setDetailLabel:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kDetails]];
	[cell setSpendId:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kSpendID]];
	[cell setAmount:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kAmount]];
	[cell setFourquareID:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kFSquareID]];
	[cell setLat:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kLat]];
	[cell setLong:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kLong]];
	[cell setCity:[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kCity]];
	[cell setUserProfile:[m_pArrFeed objectAtIndex:indexPath.row]];
	NSLog(@"foursquare id = %@",[[m_pArrFeed objectAtIndex:indexPath.row] valueForKey:kFSquareID]);
	//[cell refreshLikeFunction];
    // Configure the cell...
    
    return cell;
}



#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

